/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.loan;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.loan.Marketing;
import src.services.loan.MarketingModel;
import src.util.Messager;
/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
@ManagedBean
@SessionScoped
public class MarketingBean implements Serializable{
    private List<Marketing> marketings;  
    private Marketing selectedData, unsaveData;
    private boolean editMode = true;
    private String filter;
    private MarketingModel helper;

    public MarketingBean() {
        this.helper = new MarketingModel();
        this.marketings = helper.findAll(null);
        this.selectedData = null;
        this.unsaveData = null;
    }
    
    public boolean isEditMode() {
        return editMode;
    }
        
    public Marketing getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Marketing selectedData) {
        this.selectedData = selectedData;
    }
        
    public List<Marketing> getMarketings() {
        return this.marketings;
    }

    public Marketing getUnsaveData() {
        return unsaveData;
    }

    public void setUnsaveData(Marketing unsaveData) {
        this.unsaveData = unsaveData;
    }
        
    public String getFilter() {
        return filter;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }
    
    public void search() {
        Map<String, String> filters = new HashMap<>();
      
        filters.put("MarketingName", getFilter());
        this.marketings = helper.findAll(filters);
        setFilter("");
    }
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.unsaveData = null; 
    }
    
    public void openData() { 
        this.editMode = true;
        this.unsaveData = Marketing.clone(selectedData);
    }
    
    public void newData() {  
        this.editMode = false;
        this.selectedData = null;
        this.unsaveData = new Marketing();
    }
    
    private void save() {
        RequestContext context = RequestContext.getCurrentInstance();  
        
        boolean success = helper.saveData(unsaveData);
        if (success==true) {
            if (this.editMode==false) this.marketings.add(unsaveData);
            else {
                int i = marketings.indexOf(selectedData);
                if (i <= marketings.size()) marketings.set(i, Marketing.clone(unsaveData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) unsaveData.setMarketingId(null);
        } 
        
        if(!success) Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success = helper.deleteData(this.selectedData);
        
        if (success) {
            this.marketings.remove(this.selectedData);
            this.selectedData = null;
        }
               
        if(!success) Messager.showDeleteStatusConstaint(success);
        context.addCallbackParam("success", success);  
    }
    
    private boolean validate() {
        boolean productExist = helper.isMarketingExist(unsaveData.getMarketingName(), unsaveData.getMarketingId());
        if (productExist==true) {
            Messager.showError("nameIsExist");
        }
        return (productExist==true ? false:true);
    }
    
    public void trySave() {
        if(this.validate()) save();
    }
    
}
